package edu.mse.camel.client.posterboard.ui.treeoutline;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.gef.ui.parts.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.IPageSite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;

import com.cea.papyrus.core.dnd.TreeOutlinePageDragSourceListener;

import edu.mse.camel.client.posterboard.ui.editor.PosterBoardModelManager;
import edu.mse.camel.client.posterboard.ui.editor.dnd.PBUMLDiagramDropTargetListener;

public class PosterboardTreeOutlinePage extends ContentOutlinePage 
				implements ITabbedPropertySheetPageContributor, IAdaptable {

	public static final String ID = "PosterboardTreeOutlineView"; 
	
	private PosterBoardModelManager pbModelManager;
	private EditDomain editDomain;
	private ActionRegistry actionRegistry;
	
	private TreeViewer treeviewer;
	
	private PBUMLDiagramDropTargetListener dropListener;
	private PBTreeOutlinePageDragSourceListener dragSourceListener;
	private TreeOutlinePageDragSourceListener papyrusDragSourceListener;
	
	public PosterboardTreeOutlinePage(
			EditPartViewer viewer, EditDomain ed, PosterBoardModelManager manager, ActionRegistry ar) {
		super(viewer);
		this.pbModelManager = manager;
		this.actionRegistry = ar;
		this.editDomain = ed;
	}

	@Override
	public String getContributorId() {
		return "Posterboard Tree Outline View";
	}

	@Override
	public Object getAdapter(Class adapter) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.ui.parts.ContentOutlinePage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		// create outline viewer page
		getViewer().createControl(parent);
		// configure outline viewer
		getViewer().setEditDomain(getEditDomain());
		getViewer().setEditPartFactory(new PosterboardTreeEditpartFactory(pbModelManager));
		// configure & add context menu to viewer
		PosterboardTreeContextMenuProvider cmProvider = 
			new PosterboardTreeContextMenuProvider(
				getViewer(), getEditorActionRegistry(), pbModelManager);
		getViewer().setContextMenu(cmProvider);

		// register context menu, so it is aware of extension points org.eclipse.ui.popupmenu
		getSite().registerContextMenu(
				ID,	cmProvider, getSite().getSelectionProvider());

		// adds the common key handler to outline
		// getViewer().setKeyHandler(getCommonKeyHandler());

		// hook outline viewer
		// getSelectionSynchronizer().addViewer(getViewer());
		// initialize outline viewer with model
//		IActionBars actionBars= getSite().getActionBars();
//		actionBars.getToolBarManager().add(new SortAction());
//		actionBars.getToolBarManager().add(new DiagramViewAction());
//		actionBars.getToolBarManager().add(new ShowInheritedElementAction());
		getViewer().setContents(getModel());


		// Initialize selection in the outline
		 
		//ISelection selection = new StructuredSelection(getModel().getPBTreeEditPart());
		//getViewer().setSelection(selection);
		treeviewer = (TreeViewer) getViewer();
		setupDragAndDrop();
		
	}

	private void setupDragAndDrop() {
		//this.dropListener = new PosterBoardDropTargetListener(getViewer());
		this.dragSourceListener = new PBTreeOutlinePageDragSourceListener(getViewer());
		this.papyrusDragSourceListener = new TreeOutlinePageDragSourceListener(getViewer());
		
		getViewer().addDragSourceListener(papyrusDragSourceListener);
		getViewer().addDragSourceListener(dragSourceListener);
	}

	private PosterBoardModelManager getModel() {
		return pbModelManager;
	}

	private ActionRegistry getEditorActionRegistry() {
		return actionRegistry;
	}

	private EditDomain getEditDomain() {
		return editDomain;
	}
	
	@Override
	public void dispose() {
		getViewer().removeDragSourceListener(dragSourceListener);
		getViewer().removeDragSourceListener(papyrusDragSourceListener);
		// dispose
		super.dispose();
	}
	
	@Override
	public Control getControl() {
		return getViewer().getControl();
	}
	
	@Override
	public void init(IPageSite pageSite) {
		super.init(pageSite);
		IActionBars bars = pageSite.getActionBars();
		String id = ActionFactory.UNDO.getId();
		bars.setGlobalActionHandler(id, actionRegistry.getAction(id));
		id = ActionFactory.REDO.getId();
		bars.setGlobalActionHandler(id, actionRegistry.getAction(id));
		bars.updateActionBars();
	}

}
